package kr.hkit.powerjava.chapter10.work;

import java.util.Scanner;

public class GradeAverage {
	private int[] student; //학생
	private int stdCounter; //학생수
	private double average; //평균
	
	private Scanner input = new Scanner(System.in);

	public void ask(){
		System.out.print("학생의 수를 입력하시오: ");
		setStuCounter();
		inputGrade();
		System.out.println("성적 평균은 "+average()+"입니다.");
	}
	
	public void setStuCounter() { //학생수 설정
		stdCounter = input.nextInt();
		student = new int[stdCounter]; //학생 수만큼 학생 생성
	}
	
	public void inputGrade() {
		int grade;
		for(int i = 0 ; i < student.length; i++){
			System.out.print("학생 "+(i+1)+"의 성적을 입력하시오: ");
			grade = input.nextInt();
			if(grade > 100 || grade < 0){
				System.out.println("잘못된 성적 입니다. 다시 입력하시오.");
				i--;
				continue;
				
			}else {
				student[i] = grade;
			}

		}
	}
	
	public double average() {
		int sum = 0;
		for (int i = 0; i < student.length; i++) {
			sum = sum + student[i];
		}
		
	    this.average = sum / stdCounter;
		return average;
	}
	
}
